﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace Business
{
    public partial class Program
    {
        public static string CreateCode()
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = db.Programs.OrderByDescending(p=>p.CreatedDateTime);
                if (query.Count() > 0)
                {
                    var last = query.First();
                    string value = last.ProgramCode.Split('_')[1];
                    int _value = int.Parse(value) + 1;
                    return "PG_" + _value.ToString("00000");
                }
                else
                    return "PG_00001";
            }
            catch
            {
                return String.Empty;	
            }
            finally
            {
                db = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(ref MyProgram pg, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                Program newPG = new Program();
                newPG.ProgramCode = pg.ProgramCode;
                newPG.ProgramName = pg.ProgramName;
                newPG.BrandID = pg.BrandID;
                newPG.StartDate = pg.StartDate;
                newPG.EndDate = pg.EndDate;
                newPG.CreatedBy = userinfo.UserID;
                newPG.CreatedDateTime = DateTime.Now;
                newPG.RowVersion = 1;
                newPG.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                db.Programs.InsertOnSubmit(newPG);
                db.SubmitChanges();

                foreach (var item in pg.ListEmployeeCode)
                {
                    ProgramEmployee pe = new ProgramEmployee();
                    pe.ProgramID = newPG.ID;
                    pe.EmployeeCode = item.Trim();
                    pe.CreatedBy = userinfo.UserID;
                    pe.CreatedDateTime = DateTime.Now;
                    pe.RowVersion = 1;
                    pe.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    db.ProgramEmployees.InsertOnSubmit(pe);
                }

                db.SubmitChanges();
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
            }
            catch
            {
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetProgramByUserID(ref string messageError, ref List<MyProgram> list, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from dt in db.Programs
                            where dt.CreatedBy == userinfo.UserID
                            select new MyProgram
                            {
                                ID = dt.ID,
                                ProgramName = dt.ProgramName,
                                ProgramCode = dt.ProgramCode,
                                CustomerID = dt.Brand.CustomerID,
                                //CustomerName = dt.Brand.Customer.Name,
                                ListEmployeeCodeIE = dt.ProgramEmployees.Select(p => new SimpleEmployee { EmployeeCode = p.EmployeeCode, EmployeeName = p.Employee.EmployeeName }),
                                Status = dt.Status,
                                StartDate = dt.StartDate,
                                EndDate = dt.EndDate,
                                BrandID = dt.BrandID,
                            };
                list = query.ToList();
                foreach (var item in list)
                {
                    foreach (var cs in item.ListEmployeeCodeIE)
                    {
                        if (item.Field_CS == null)
                            item.Field_CS += cs.EmployeeCode;
                        else
                            item.Field_CS += "," + cs.EmployeeCode;
                    }
                }
                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetProgramByCSID(ref string messageError, ref List<MyProgram> list, Common.UserInfo userinfo , int Status = 1)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = from dt in db.Programs
                            where dt.ProgramEmployees.Any(x => x.EmployeeCode == userinfo.UserCode)
                            && dt.Status == Status
                            select new MyProgram
                            {
                                ID = dt.ID,
                                ProgramName = dt.ProgramName,
                                ProgramCode = dt.ProgramCode,
                                CustomerID = dt.Brand.CustomerID,
                                CustomerName = dt.Brand.Customer.Name,
                                ListEmployeeCodeIE = dt.ProgramEmployees.Select(p => new SimpleEmployee { EmployeeCode = p.EmployeeCode, EmployeeName = p.Employee.EmployeeName }),
                                Status = dt.Status,
                                StartDate = dt.StartDate,
                                EndDate = dt.EndDate,
                                BrandID = dt.BrandID,
                                BrandName = dt.Brand.BrandName,
                                CreatedBy = dt.CreatedBy,
                                CreatedDateTime = dt.CreatedDateTime,
                            };
                list = query.OrderByDescending(p => p.CreatedDateTime).ToList();
                foreach (var item in list)
                {
                    foreach (var cs in item.ListEmployeeCodeIE)
                    {
                        if (item.Field_CS == null)
                            item.Field_CS += cs.EmployeeCode;
                        else
                            item.Field_CS += "," + cs.EmployeeCode;
                    }
                }
                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE Update(ref string messageError, ref MyProgram pg, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                long ID = pg.ID;

                //Update Program
                Program _pg = db.Programs.Single(p => p.ID == ID);
                _pg.ProgramName = pg.ProgramName;
                _pg.BrandID = pg.BrandID;
                _pg.StartDate = pg.StartDate;
                _pg.EndDate = pg.EndDate;
                _pg.LastUpdatedBy = userinfo.UserID;
                _pg.LastUpdatedDateTime = DateTime.Now;
                _pg.RowVersion += 1;

                //Delete Old List CS
                var oldListCS = db.ProgramEmployees.Where(p => p.ProgramID == ID);
                db.ProgramEmployees.DeleteAllOnSubmit(oldListCS);

                //Create New List CS
                var newlistCS = pg.Field_CS.Split(',');
                foreach (var item in newlistCS)
                {
                    ProgramEmployee pe = new ProgramEmployee();
                    pe.ProgramID = ID;
                    pe.EmployeeCode = item.Trim();
                    pe.CreatedBy = userinfo.UserID;
                    pe.CreatedDateTime = DateTime.Now;
                    pe.RowVersion = 1;
                    pe.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    db.ProgramEmployees.InsertOnSubmit(pe);
                }

                db.SubmitChanges();
                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }
    }

    public partial class MyProgram : Business.Program
    {
        public List<string> ListEmployeeCode { get; set; }
        public string Field_CS { get; set; }
        public int CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string BrandName { get; set; }
    }

    public partial class Program
    {
        public IEnumerable<SimpleEmployee> ListEmployeeCodeIE { get; set; }
        public string StatusConvert
        {
            get
            {
                Common.STATUS_CODE.ROW_STATUS foo = (Common.STATUS_CODE.ROW_STATUS)Status;
                return Common.EnumUtils.stringValueOf(foo);
            }
        }
    }
}
